package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLocation;

import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.soul.UserLocationSoul;
import com.itheima.soul.UserSiteVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    UserInfoService userInfoService;


    @DubboReference
    UserLocationService userLocationService;


    public ResponseEntity findUserAll(Integer pageNum,Integer pageSize,String title) {

        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize,title);


        List<UserInfo> items = (List<UserInfo>) pageBeanVo.getItems();

        List<UserLocationSoul> locationSouls = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {

            for (UserInfo item : items) {

                UserLocationSoul locationSoul = new UserLocationSoul();
                UserSiteVo userSiteVo = userLocationService.findByUserId(item.getId());


                locationSoul.setId(item.getId());
                locationSoul.setAvatar(item.getAvatar());
                locationSoul.setGender(item.getGender());

                if (userSiteVo==null) {
                    userSiteVo = new UserSiteVo();
                    userSiteVo.setAddress("中国北京市昌平区建材城西路16号");
                }

                locationSoul.setAddress(userSiteVo.getAddress());


                locationSoul.setNickname(item.getNickname());
                locationSouls.add(locationSoul);

            }


        }
        pageBeanVo.setItems(locationSouls);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findSite(Long id) {

        UserSiteVo userSiteVo = userLocationService.findByUserId(id);

        UserInfo userInfo = userInfoService.findByUserId(id);


        userSiteVo.setLongitude(userSiteVo.getLongitude());
        userSiteVo.setLatitude(userSiteVo.getLatitude());
        userSiteVo.setAddress(userSiteVo.getAddress());
        userSiteVo.setId(userInfo.getId());
        userSiteVo.setAvatar(userInfo.getAvatar());
        userSiteVo.setNickname(userInfo.getNickname());

        return ResponseEntity.ok(userSiteVo);
    }

}
